#!/bin/bash

# 批量启动一主二备拓扑的所有虚拟机

set -e

SOURCE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SOURCE_DIR/config.conf"

echo "启动一主二备拓扑所有虚拟机..."
echo

# 定义节点启动顺序（先启动master）
NODES=("${VM_NAME_PREFIX}-master1" "${VM_NAME_PREFIX}-worker1" "${VM_NAME_PREFIX}-worker2")

for vm_name in "${NODES[@]}"; do
    start_script="$VM_IMAGE_DIR/start-${vm_name}.sh"
    if [ -f "$start_script" ]; then
        echo "启动 $vm_name..."
        "$start_script"
        sleep 2  # 等待2秒再启动下一个
    else
        echo "警告: $start_script 不存在"
    fi
done

echo
echo "所有虚拟机已启动"
echo "虚拟机状态:"
for vm_name in "${NODES[@]}"; do
    pid_file="$VM_IMAGE_DIR/${vm_name}.pid"
    if [ -f "$pid_file" ] && kill -0 "$(cat "$pid_file")" 2>/dev/null; then
        echo "✓ $vm_name: 运行中"
    else
        echo "✗ $vm_name: 未运行"
    fi
done
